<?php
/**
 * @file profile.php
 * @brief
 * 	add / edit master profile and / or pet profile
 */
require_once ('../config.php');
require_once ('functions.php');
class ProfileManager {
	public $user_id;

	public $details;
	public $master_details;
	
	
	public function ProfileManager($user_id) {
		$this -> user_id = $user_id;
		$this -> details = $this->getProfile($user_id);
		$this -> master_details = $this->getMasterProfile($this->details['master_profile_id']);
	}

	public function getProfile($user_id) {
		DB::query('set names utf8');//needed for utf8 encoding issue (display ???? on page)
		return DB::queryFirstRow('SELECT * FROM `profile` WHERE `user_id`=%i', $user_id);
	}
	
	public function getMasterProfile($master_profile_id) {
		DB::query('set names utf8');
		return DB::queryFirstRow('SELECT * FROM `master_profile` WHERE `id`=%i', $master_profile_id);
	}
	
	public function getMasterName(){
		return $this->master_details['name'];
	}
	public function getMasterDOB(){
		return $this->master_details['dob'];
	}
	/**
	 * reutrn the sex of master
	 * @return 
	 * 	‘男 ’or ‘女’
	 */
	public function getMasterSex(){
		return ($this->master_details['sex'] == 'M') ? '男':'女';
	}
	
	public function getMasterXingzuo(){
		
	}
	
	
	/**
	 * print a <ul> with profile of this pet
	 */
	public function printBrief($class='') {
		echo '<ul class="'.$class.'">';
		echo '<li>'.$this->details['name'].'</li>';
		echo '<li>'.$this->details['avatar'].'</li>';
		echo '<li>'.$this->details['dob'].'</li>';
		echo '<li>'.$this->details['dating_status'].'</li>';
		echo '<li>'.$this->details['description'].'</li>';
		echo '<ul>';
	}
	/**
	 * print master profile brief on the left panel
	 */
	function printMasterBrief(){
	
	}
	
	/**
	 * add a location
	 */
	function addLocation($suburb, $city, $state, $country) {
		DB::insert('location', array('suburb' => $suburb, 'city' => $city, 'state' => $state, 'country' => $country));
		return DB::insertId();
	}

	/**
	 * delete a location in db
	 */
	function deleteLocation($id) {
		DB::delete('location', "id=%i", $id);
	}

	function addSpecies($name) {
		DB::insert('species', array('species' => $name));
		return DB::insertId();
	}

	function deleteSpecies($id) {
		DB::delete('species', "id=%i", $id);
		return DB::affectedRows();
	}

	function addBreed($name, $species_id) {
		DB::insert('breed', array('description' => $name, 'species_id' => $species_id));
		return DB::insertId();
	}

	function deleteBreed($id) {
		DB::delete('breed', "id=%i", $id);
		return DB::affectedRows();
	}

	function addMasterProfile($name, $dob, $sex, $location, $star, $avatar) {
		DB::insert('master_profile', array('name' => $name, 'dob' => $dob, 'sex' => $sex, 'location_id' => $location, 'star' => $star, 'avatar' => $avatar));
		return DB::insertId();
	}

	function addPetProfile($name, $dob, $sex, $master_id, $avatar, $species_id, $breed_id, $dating_status, $description) {
		DB::insert('pet_profile', array('name' => $name, 'dob' => $dob, 'sex' => $sex, 'master_id' => $master_id, 'avatar' => $avatar, 'species_id' => $species_id, 'breed_id' => $breed_id, 'dating_status' => $dating_status, 'description' => $description));
	}

	/**
	 * edit master profile
	 *
	 */
	function editMaster($id) {

	}

	/**
	 * edit pet profile
	 */
	function editPet($id) {

	}

	/**
	 * get one pet by id
	 */
	function getPetById($pet_id) {
		DB::query("SET NAMES 'utf8'");
		return DB::queryFirstRow("SELECT * FROM `pet_profile` WHERE `id`=%i", $pet_id);
	}

	/**
	 * get one pet by id
	 */
	function getOnePet($master_id) {
		DB::query("SET NAMES 'utf8'");
		return DB::queryFirstRow("SELECT * FROM `pet_profile` WHERE `master_id`=%i", $master_id);
	}

	/**
	 * get pets by master id
	 */
	function getPets($master_id) {
		DB::query("SET NAMES 'utf8'");
		return DB::query("SELECT * FROM `pet_profile` WHERE `master_id`=%i", $master_id);
	}

	/**
	 * get master profile by email
	 */
	function getMasterByEmail($email) {
		$user = DB::queryFirstRow("SELECT * FROM `account` WHERE `email`=%s", $email);
		return DB::queryFirstRow("SELECT * FROM `master_profile` WHERE `id`=%i", $user['master_id']);
	}

	/**
	 * get master profile by id
	 */
	function getMasterById($id) {
		return DB::queryFirstRow("SELECT * FROM `master_profile` WHERE `id`=%i", $id);
	}

	function masterHasPet($master_id, $pet_id) {
		$db_master_id = DB::queryOneField('master_id', "SELECT `master_id` FROM `pet_profile` WHERE id=%i", $pet_id);
		return $master_id == $db_master_id;
	}

}
?>